<?php
namespace App\Model\Phalapialiyunsms;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

class Main {
    
    
    protected $accessKeyId;
    protected $accessSecret;
    
    public static function init($cfg) {
        $obj = new self();
        $obj->accessKeyId = $cfg['accessKeyId'];
        $obj->accessSecret = $cfg['accessSecret'];
        return $obj;
    }
    
    /**
     * @link Download：https://github.com/aliyun/openapi-sdk-php
     * @link Usage：https://github.com/aliyun/openapi-sdk-php/blob/master/README.md
     * @link 代码生成：https://api.aliyun.com/?spm=a2c4g.11186623.2.13.4be74e6aFKEKXZ#/?product=Dysmsapi&lang=PHP
     */
    public function aliyunSendSms($PhoneNumbers, $SignName, $TemplateCode, $TemplateParam) {
        AlibabaCloud::accessKeyClient($this->accessKeyId, $this->accessSecret)
        ->regionId('cn-hangzhou') // replace regionId as you need
        ->asDefaultClient();
        
        $di = \PhalApi\DI();
        
        $query = array(
            'RegionId' => "default",
            'PhoneNumbers' => $PhoneNumbers,
            'SignName' => $SignName,
            'TemplateCode' => $TemplateCode,
        );
        if (!empty($TemplateParam)) {
            $query['TemplateParam'] = json_encode($TemplateParam);
        }
        
        try {
            
            $result = AlibabaCloud::rpc()
            ->product('Dysmsapi')
            // ->scheme('https') // https | http
            ->version('2017-05-25')
            ->action('SendSms')
            ->method('POST')
            ->host('dysmsapi.aliyuncs.com')
            ->options([
                'query' => $query,
            ])
            ->request();
            $rs = $result->toArray();
            
            $di->logger->info('阿里云短信发送成功', array('rs' => $rs, 'query' => $query));
            
            return $rs;
        } catch (ClientException $e) {
            $di->logger->error('阿里云短信发送失败', array('error' => $e->getErrorMessage(), 'query' => $query));
            throw new \Exception('客户端异常：' . $e->getErrorMessage());
        } catch (ServerException $e) {
            $di->logger->error('阿里云短信发送失败', array('error' => $e->getErrorMessage(), 'query' => $query));
            throw new \Exception('服务端异常：' . $e->getErrorMessage());
        }
    }
    
    public function aliyunQuerySendDetails($CurrentPage, $PageSize, $PhoneNumber, $SendDate) {
        AlibabaCloud::accessKeyClient($this->accessKeyId, $this->accessSecret)
        ->regionId('cn-hangzhou') // replace regionId as you need
        ->asDefaultClient();
        
        $query = array(
            'RegionId' => "default",
            'PhoneNumber' => $PhoneNumber,
            'SendDate' => $SendDate,
            'PageSize' => $PageSize,
            'CurrentPage' => $CurrentPage,
        );
        
        try {
            
            $result = AlibabaCloud::rpc()
            ->product('Dysmsapi')
            // ->scheme('https') // https | http
            ->version('2017-05-25')
            ->action('QuerySendDetails')
            ->method('POST')
            ->host('dysmsapi.aliyuncs.com')
            ->options([
                'query' => $query,
            ])
            ->request();
            $rs = $result->toArray();
            
            return $rs;
        } catch (ClientException $e) {
            throw new \Exception('客户端异常：' . $e->getErrorMessage());
        } catch (ServerException $e) {
            throw new \Exception('服务端异常：' . $e->getErrorMessage());
        }
    }
}
